<?php
declare(strict_types=1);
 
namespace App\JsonRpc\ServiceProvider;

use Hyperf\RpcServer\Annotation\RpcService;
use App\Service\CouponService as Coupon;
use App\Utils\ServerUtil;
use App\Utils\ApiUtils;
use Hyperf\Utils\ApplicationContext;
use App\Validate\CouponValidation;
use App\Constants\ErrorCode;
/**
 * 注意，如希望通过服务中心来管理服务，需在注解内增加 publishTo 属性
 * @RpcService(name="CouponService", protocol="jsonrpc", server="jsonrpc")
 */
class CouponService implements CouponServiceInterface
{
    public function consume($data): string{
        $data = json_decode($data,true); 
        ServerUtil::info('rpc-CouponService-进入', ['param'=>$data]);
        $type = $data['type'] ?? '';
        make(CouponValidation::class)->check($data, 'rpcCouponService')->response();
        $obj =  ApplicationContext::getContainer()->get(Coupon::class);
        switch ($type)
        {
            case 1: 
                //标记红包使用
                $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],1,$data['order_no']??"",$data['type_explain']??0);
                ServerUtil::info('rpc-CouponService-标记红包使用',['param'=>$data,'res'=>$res]);
                $responseData=json_encode(ApiUtils::send($res['code'],$res['msg']));  
                break;
            case 2: 
                //领取红包
                $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],2,$data['order_no']??"");
                ServerUtil::info('rpc-CouponService-领取红包',['param'=>$data,'res'=>$res]);
                $responseData=json_encode(ApiUtils::send($res['code'],$res['msg']));  
            break;
            case 3: 
                //退款红包
                $res = $obj->updateCoupon($data['openid'],$data['coupon_id'],3,$data['order_no']??"",$data['type_explain']??0);
                ServerUtil::info('rpc-CouponService-退款红包',['param'=>$data,'res'=>$res]);
                $responseData=json_encode(ApiUtils::send($res['code'],$res['msg']));  
                break;
            default:
            $responseData=json_encode(ApiUtils::send(ERRORCODE::ERR_BUESSUS,'type解析失败')); 
        }
        return $responseData;
    }
}
